package cn.edu.dlut.port.isp.profile;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Serializable;
import java.net.URL;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class UserProfileSession implements Serializable {

	private static final long serialVersionUID = 3019640172632507209L;
	private String name;
	private String email;
	private LocalDate birthdate;
	private List<String> interests = new ArrayList<>();
	private URL picturePath;

	public void saveForm(ProfileForm profileForm) {
		this.name = profileForm.getName();
		this.email = profileForm.getEmail();
		this.birthdate = profileForm.getBirthdate();
		this.interests = profileForm.getInterests();
	}

	public ProfileForm toForm() {
		ProfileForm profileForm = new ProfileForm();
		profileForm.setName(name);
		profileForm.setEmail(email);
		profileForm.setBirthdate(birthdate);
		profileForm.setInterests(interests);
		return profileForm;
	}

	public void setPicturePath(Resource picturePath) throws IOException {
		this.picturePath = picturePath.getURL();
	}

	public Resource getPicturePath() {
		return picturePath == null ? null : new UrlResource(picturePath);
	}

	public List<String> getInterests() {
		return interests;
	}

	public void setInterests(List<String> interests) {
		this.interests = interests;
	}
}
